import tkinter as tk
from tkinter import filedialog
from docx2pdf import convert

class WordToPdf:
    def __init__(self, window):
        self.window = window
        self.filepath = tk.StringVar()
        self.init()
    def init(self):
        tk.Label(text='word转PDF', font=('', 16)).place(x = 10, y=230)
        tk.Entry(self.window,textvariable=self.filepath, width=20).place(x=50, y=255)
        tk.Button(self.window, text='打开docx文件(保存到当前文件夹)', command=self.open_file).place(x=200, y=250)

    def open_file(self):
        file_path = filedialog.askopenfilename(title='选择文件')
        if file_path:
            self.filepath.set(file_path)
            try:
                filename = file_path.split(".docx")[0]
                convert(file_path, f"{filename}.pdf")
            except Exception as e:
                print(e)




